package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HhOrderDetailMapper;
import com.ruoyi.system.domain.HhOrderDetail;
import com.ruoyi.system.service.IHhOrderDetailService;

/**
 * 销售订单明细Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-06-15
 */
@Service
public class HhOrderDetailServiceImpl implements IHhOrderDetailService 
{
    @Autowired
    private HhOrderDetailMapper hhOrderDetailMapper;

    /**
     * 查询销售订单明细
     * 
     * @param odId 销售订单明细ID
     * @return 销售订单明细
     */
    @Override
    public HhOrderDetail selectHhOrderDetailById(Long odId)
    {
        return hhOrderDetailMapper.selectHhOrderDetailById(odId);
    }

    /**
     * 查询销售订单明细列表
     * 
     * @param hhOrderDetail 销售订单明细
     * @return 销售订单明细
     */
    @Override
    public List<HhOrderDetail> selectHhOrderDetailList(HhOrderDetail hhOrderDetail)
    {
        return hhOrderDetailMapper.selectHhOrderDetailList(hhOrderDetail);
    }

    /**
     * 新增销售订单明细
     * 
     * @param hhOrderDetail 销售订单明细
     * @return 结果
     */
    @Override
    public int insertHhOrderDetail(HhOrderDetail hhOrderDetail) {
        //检查 订单号  商品编号  金额
        String orderid = hhOrderDetail.getOrderId();
        Long productId = hhOrderDetail.getProductId();
        int num = Integer.parseInt(hhOrderDetail.getProductNum());
        //如果商品不存在直接退出
        if (hhOrderDetailMapper.selectproductId(productId) <= 0) {
            return 0;
        }
        //订单不存在退出
        if (hhOrderDetailMapper.selectorderId(orderid) <= 0) {
            return 0;
        }
        //通过商品编号去拿单位
        hhOrderDetail.setProductUnit(hhOrderDetailMapper.getunit(productId));
        //获取单价
        double detailPrice = 0;
        detailPrice = hhOrderDetailMapper.getprice(productId);
        hhOrderDetail.setDetailPrice(String.valueOf(detailPrice));
        //输入金额
        double detailAmount = 0;
        detailAmount = num * detailPrice;
        hhOrderDetail.setDetailAmount(String.valueOf(detailAmount));
        //通过订单号去更新订单信息
        hhOrderDetailMapper.updatemenoy(orderid, detailAmount);


        hhOrderDetail.setCreateTime(DateUtils.getNowDate());
        int fl = hhOrderDetailMapper.insertHhOrderDetail(hhOrderDetail);

        //返回所有同订单的金额加起来更新数据库大订单数据
        List<String> allmoney = hhOrderDetailMapper.selectall(orderid);
        double amount = 0;
        for (int i = 0; i < allmoney.size(); i++) {
            amount += Double.parseDouble(allmoney.get(i));
        }
        String totalmoney = String.valueOf(amount);
        hhOrderDetailMapper.updateallmoney(totalmoney, orderid);

        return fl;
    }

    /**
     * 修改销售订单明细
     * 
     * @param hhOrderDetail 销售订单明细
     * @return 结果
     */
    @Override
    public int updateHhOrderDetail(HhOrderDetail hhOrderDetail)
    {
        //检查 订单号  商品编号  金额
        String orderid = hhOrderDetail.getOrderId();
        Long productId = hhOrderDetail.getProductId();
        int num = Integer.parseInt(hhOrderDetail.getProductNum());
        //如果商品不存在直接退出
        if (hhOrderDetailMapper.selectproductId(productId)<=0){
            return 0;
        }
        //订单不存在退出
        if (hhOrderDetailMapper.selectorderId(orderid)<=0){
            return 0;
        }
        //通过商品编号去拿单位
        hhOrderDetail.setProductUnit(hhOrderDetailMapper.getunit(productId));
        //获取单价
        double detailPrice = hhOrderDetailMapper.getprice(productId);
        hhOrderDetail.setDetailPrice(String.valueOf(detailPrice));
        //输入金额
        double detailAmount = num*detailPrice;
        hhOrderDetail.setDetailAmount(String.valueOf(detailAmount));
        //通过订单号去更新订单信息
        hhOrderDetailMapper.updatemenoy(orderid,detailAmount);


        hhOrderDetail.setUpdateTime(DateUtils.getNowDate());
        int fl = hhOrderDetailMapper.updateHhOrderDetail(hhOrderDetail);

        //返回所有同订单的金额加起来更新数据库大订单数据
        List<String> allmoney = hhOrderDetailMapper.selectall(orderid);
        double amount=0;
        for (int i = 0; i < allmoney.size(); i++) {
            amount += Double.parseDouble(allmoney.get(i));
        }
        hhOrderDetailMapper.updateallmoney(String.valueOf(amount),orderid);

        return fl;
    }

    /**
     * 批量删除销售订单明细
     * 
     * @param odIds 需要删除的销售订单明细ID
     * @return 结果
     */
    @Override
    public int deleteHhOrderDetailByIds(Long[] odIds)
    {
        return hhOrderDetailMapper.deleteHhOrderDetailByIds(odIds);
    }

    /**
     * 删除销售订单明细信息
     * 
     * @param odId 销售订单明细ID
     * @return 结果
     */
    @Override
    public int deleteHhOrderDetailById(Long odId)
    {
        return hhOrderDetailMapper.deleteHhOrderDetailById(odId);
    }
}
